package com.sinodata.bsm.cicp.collector.was7;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.j2ee.statistics.BoundedRangeStatistic;
import javax.management.j2ee.statistics.CountStatistic;
import javax.management.j2ee.statistics.Statistic;
import javax.management.j2ee.statistics.Stats;
import javax.management.remote.JMXConnector;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.util.JmxUtil;
import com.sinodata.bsm.common.vo.ResPropertyValue;

public class JVMWas7Collector extends Collector {

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        List<ResPropertyValue> value = new ArrayList<ResPropertyValue>();
        ResPropertyValue propertyValueX = new ResPropertyValue();
        String[] propMetas = getPropMeta();
        Stats stats;
        ResPropertyValue.PropValue[] proValues = new ResPropertyValue.PropValue[getPropMeta().length];
        String serverName = this.getParamMap().get("serverName");
        String nodeName = this.getParamMap().get("nodeName");
        JMXConnector connector = null;
        try {
            String query = "WebSphere:type=JVM,process=" + serverName + ",node=" + nodeName + ",*";
            ObjectName queryName = new ObjectName(query);
            //            connector = JmxUtil.getConnection(JmxUtil.WEBSPHERE7, ip + ":" + this.getParamMap().get("serverPort"));
            connector = JmxUtil.getConnection(ip, Integer.parseInt(this.getParamMap().get("serverPort")));
            MBeanServerConnection mbsc = connector.getMBeanServerConnection();
            Set s = mbsc.queryNames(queryName, null);
            for (Iterator obj = s.iterator(); obj.hasNext();) {
                ObjectName nodeAgent = (ObjectName) obj.next();
                stats = (Stats) mbsc.getAttribute(nodeAgent, "stats");
                Statistic[] data = stats.getStatistics();
                for (int i = 0; i < data.length; i++) {
                    if (data[i] instanceof BoundedRangeStatistic) {
                        BoundedRangeStatistic heapSize = (BoundedRangeStatistic) data[i];
                        proValues[0] = ResPropertyValue.createPropValue(heapSize.getUpperBound() + "", propMetas[0]);
                        proValues[1] = ResPropertyValue.createPropValue(heapSize.getCurrent() + "", propMetas[1]);
                    } else if (data[i] instanceof CountStatistic) {
                        CountStatistic common = (CountStatistic) data[i];
                        if ("UsedMemory".equals(common.getName())) {
                            proValues[2] = ResPropertyValue.createPropValue(common.getCount() + "", propMetas[2]);
                        } else if ("UpTime".equals(common.getName())) {
                            proValues[3] = ResPropertyValue.createPropValue(common.getCount() + "", propMetas[3]);
                        } else if ("ProcessCpuUsage".equals(common.getName())) {
                            proValues[4] = ResPropertyValue.createPropValue(common.getCount() + "", propMetas[4]);
                        }
                    }
                    propertyValueX.setValues(proValues);
                    value.add(propertyValueX);
                }
            }

        } catch (Exception e) {
            logger.error("failed to collect by JVMCollector", e);
            throw new CollectorException(e);
        } finally {
            JmxUtil.closeConnector(connector);
        }
        System.out.println(value.size());
        return value;
    }

    @Override
    public String[] getParamMeta() {
        //        return new String[] { "服务器端口", "服务器名", "节点名" };
        return new String[] { "serverPort", "serverName", "nodeName" };
    }

    @Override
    public String[] getPropMeta() {
        //      return new String[] { "JVM总内存上限", "JVM总内存(HeapSize)", "JVM已用内存", "JVM运行的时间", "JVM占用cpu率" };
        return new String[] { "TotalJVMUpperLimit", "TotalJVMMemory", "JVMUsedMemory", "JVMRuntime", "JVMOccupyCPURatio" };
    }

}
